/*
 * Created by JFormDesigner on Mon Jan 06 21:19:24 CST 2025
 */

package org.common.utils.generatormapper.window;
import javax.swing.event.*;
import org.common.utils.generatormapper.GeneratorMapperManager;
import org.common.utils.generatormapper.event.EventManager;
import org.common.utils.generatormapper.event.entity.*;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import org.jdesktop.beansbinding.*;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;

/**
 * @author zhouzhibing
 */
public class GeneratorMapperWindow extends JFrame {
    public GeneratorMapperManager getManager() {
        return GeneratorMapperManager.getInstance();
    }

    public GeneratorMapperWindow() {
        GeneratorMapperManager.getInstance().initBefore(this);
        initComponents();
        GeneratorMapperManager.getInstance().initAfter(this);
    }

    private void tblListValueChanged(ListSelectionEvent e) {
        EventManager.trigger(new SelectTableEvent(e));
    }

    private void btnGeneratorMousePressed(MouseEvent e) {
        EventManager.trigger(new GeneratorEvent(tblList));
    }

    private void txtGeneratorPathMousePressed(MouseEvent e) {
        EventManager.trigger(new SelectDirEvent(txtGeneratorPath , this));
    }

    private void thisWindowClosing(WindowEvent e) {
        EventManager.trigger(new CloseEvent());
    }

    private void btnConnectMousePressed(MouseEvent e) {
        EventManager.trigger(new MysqlConnectEvent(txtHost , txtUsername , txtPassword));
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        ResourceBundle bundle = ResourceBundle.getBundle("localization");
        inputPanel = new JPanel();
        lblHost = new JLabel();
        txtHost = new JTextField();
        lblUsername = new JLabel();
        txtUsername = new JTextField();
        lblPassword = new JLabel();
        txtPassword = new JPasswordField();
        btnConnect = new JButton();
        btnGenerator = new JButton();
        txtGeneratorPath = new JTextField();
        lblGeneratorPath = new JLabel();
        txtGeneratorPackage = new JTextField();
        lblGeneratorPackage = new JLabel();
        pnlPrintln = new JScrollPane();
        txtAreaLog = new JTextArea();
        scrollPane2 = new JScrollPane();
        tblList = new JList();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle(bundle.getString("GeneratorEntityWindow.this.title"));
        setFont(new Font("Al Bayan", Font.PLAIN, 11));
        setName("mainWindow");
        setResizable(false);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                thisWindowClosing(e);
            }
        });
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== inputPanel ========
        {
            inputPanel.setLayout(null);

            //---- lblHost ----
            lblHost.setText(bundle.getString("GeneratorEntityWindow.lblHost.text"));
            lblHost.setFont(new Font("Al Bayan", Font.PLAIN, 11));
            lblHost.setName("lblHost");
            lblHost.setHorizontalAlignment(SwingConstants.RIGHT);
            inputPanel.add(lblHost);
            lblHost.setBounds(new Rectangle(new Point(13, 13), lblHost.getPreferredSize()));

            //---- txtHost ----
            txtHost.setName("txtHost");
            inputPanel.add(txtHost);
            txtHost.setBounds(80, 5, 435, 30);

            //---- lblUsername ----
            lblUsername.setText(bundle.getString("GeneratorEntityWindow.lblUsername.text"));
            lblUsername.setFont(new Font("Al Bayan", Font.PLAIN, 11));
            lblUsername.setName("lblUsername");
            lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
            inputPanel.add(lblUsername);
            lblUsername.setBounds(265, 49, 60, 14);

            //---- txtUsername ----
            txtUsername.setName("txtUsername");
            txtUsername.setText("root");
            inputPanel.add(txtUsername);
            txtUsername.setBounds(80, 40, 180, 30);

            //---- lblPassword ----
            lblPassword.setText(bundle.getString("GeneratorEntityWindow.lblPassword.text"));
            lblPassword.setFont(new Font("Al Bayan", Font.PLAIN, 11));
            lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
            inputPanel.add(lblPassword);
            lblPassword.setBounds(10, 50, 60, 14);

            //---- txtPassword ----
            txtPassword.setName("txtPassword");
            txtPassword.setText("123456");
            inputPanel.add(txtPassword);
            txtPassword.setBounds(335, 40, 180, 30);

            //---- btnConnect ----
            btnConnect.setText("\u8fde\u63a5");
            btnConnect.setName("btnConnect");
            btnConnect.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    btnConnectMousePressed(e);
                }
            });
            inputPanel.add(btnConnect);
            btnConnect.setBounds(540, 10, 100, 60);

            //---- btnGenerator ----
            btnGenerator.setText(bundle.getString("GeneratorEntityWindow.btnGenerator.text"));
            btnGenerator.setName("btnGenerator");
            btnGenerator.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    btnGeneratorMousePressed(e);
                }
            });
            inputPanel.add(btnGenerator);
            btnGenerator.setBounds(540, 87, 100, 60);

            //---- txtGeneratorPath ----
            txtGeneratorPath.setName("txtGeneratorPath");
            txtGeneratorPath.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    txtGeneratorPathMousePressed(e);
                }
            });
            inputPanel.add(txtGeneratorPath);
            txtGeneratorPath.setBounds(80, 85, 435, 30);

            //---- lblGeneratorPath ----
            lblGeneratorPath.setText(bundle.getString("GeneratorEntityWindow.lblGeneratorPath.text"));
            lblGeneratorPath.setFont(new Font("Al Bayan", Font.PLAIN, 11));
            lblGeneratorPath.setName("lblUsername");
            lblGeneratorPath.setHorizontalAlignment(SwingConstants.RIGHT);
            inputPanel.add(lblGeneratorPath);
            lblGeneratorPath.setBounds(10, 95, 60, 14);

            //---- txtGeneratorPackage ----
            txtGeneratorPackage.setName("txtGeneratorPackage");
            inputPanel.add(txtGeneratorPackage);
            txtGeneratorPackage.setBounds(80, 120, 435, 30);

            //---- lblGeneratorPackage ----
            lblGeneratorPackage.setText(bundle.getString("GeneratorEntityWindow.lblGeneratorPackage.text"));
            lblGeneratorPackage.setFont(new Font("Al Bayan", Font.PLAIN, 11));
            lblGeneratorPackage.setName("lblUsername");
            lblGeneratorPackage.setHorizontalAlignment(SwingConstants.RIGHT);
            inputPanel.add(lblGeneratorPackage);
            lblGeneratorPackage.setBounds(10, 130, 60, 14);
        }
        contentPane.add(inputPanel);
        inputPanel.setBounds(285, 10, 650, 155);

        //======== pnlPrintln ========
        {

            //---- txtAreaLog ----
            txtAreaLog.setName("txtAreaLog");
            txtAreaLog.setFont(new Font("Al Bayan", Font.PLAIN, 10));
            pnlPrintln.setViewportView(txtAreaLog);
        }
        contentPane.add(pnlPrintln);
        pnlPrintln.setBounds(285, 175, 635, 360);

        //======== scrollPane2 ========
        {

            //---- tblList ----
            tblList.setName("tblList");
            tblList.addListSelectionListener(e -> tblListValueChanged(e));
            scrollPane2.setViewportView(tblList);
        }
        contentPane.add(scrollPane2);
        scrollPane2.setBounds(15, 10, 260, 525);

        contentPane.setPreferredSize(new Dimension(935, 580));
        pack();
        setLocationRelativeTo(null);

        //---- bindings ----
        bindingGroup = new BindingGroup();
        bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            this, BeanProperty.create("manager.dataManager.dataEntity.javaDir"),
            txtGeneratorPath, BeanProperty.create("text")));
        bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            this, BeanProperty.create("manager.dataManager.dataEntity.packageName"),
            txtGeneratorPackage, BeanProperty.create("text")));
        bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            this, BeanProperty.create("manager.dataManager.dataEntity.mysqlHost"),
            txtHost, BeanProperty.create("text")));
        bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            this, BeanProperty.create("manager.dataManager.dataEntity.mysqlUsername"),
            txtUsername, BeanProperty.create("text")));
        bindingGroup.addBinding(Bindings.createAutoBinding(UpdateStrategy.READ_WRITE,
            this, BeanProperty.create("manager.dataManager.dataEntity.mysqlPassword"),
            txtPassword, BeanProperty.create("text")));
        bindingGroup.bind();
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel inputPanel;
    private JLabel lblHost;
    private JTextField txtHost;
    private JLabel lblUsername;
    private JTextField txtUsername;
    private JLabel lblPassword;
    private JPasswordField txtPassword;
    private JButton btnConnect;
    private JButton btnGenerator;
    private JTextField txtGeneratorPath;
    private JLabel lblGeneratorPath;
    private JTextField txtGeneratorPackage;
    private JLabel lblGeneratorPackage;
    private JScrollPane pnlPrintln;
    private JTextArea txtAreaLog;
    private JScrollPane scrollPane2;
    private JList tblList;
    private BindingGroup bindingGroup;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
